package com.isoft.demo.service.impl;

import com.isoft.demo.common.ResponseData;
import com.isoft.demo.dao.CustomerDao;
import com.isoft.demo.dao.CustomerRolersDao;
import com.isoft.demo.dao.PermissionRolersDao;
import com.isoft.demo.entity.CustomerInfo;
import com.isoft.demo.entity.CustomerRolers;
import com.isoft.demo.entity.PermissionRolers;
import com.isoft.demo.service.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public  class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerDao customerDao;

    @Resource
    private CustomerRolersDao dao;

    @Resource
    private PermissionRolersDao prdao;

    @Override
    public ResponseData addCustomer(CustomerInfo customerInfo){
        int n = customerDao.addCustomer(customerInfo);
        ResponseData rs = new ResponseData();
        List list = new ArrayList();
        if(n > 0){
            rs.setCode(0);
            rs.setMsg("添加成功");
            list.add(customerInfo);
            rs.setData(list);
        } else{
            rs.setCode(1);
            rs.setMsg("添加失败");
        }
        return rs;
    }

    @Override
    public ResponseData updateCustomer(CustomerInfo customerInfo) {
        int m = customerDao.updateCustomer(customerInfo);
        ResponseData rs = new ResponseData();
        if(m != 0){
            rs.setCode(0);
            rs.setMsg("修改成功！");
        } else{
            rs.setCode(1);
            rs.setMsg("修改失败!");
        }
        return rs;
    }

    @Override
    public ResponseData updateCus(CustomerInfo customerInfo) {
        int m = customerDao.updateCus(customerInfo);
        ResponseData rs = new ResponseData();
        if(m != 0){
            rs.setCode(0);
            rs.setMsg("修改成功！");
        } else{
            rs.setCode(1);
            rs.setMsg("修改失败!");
        }
        return rs;
    }

    @Override
    public ResponseData deleteCustomer(int cid) {
        int a = customerDao.deleteCustomer(cid);
        ResponseData rs = new ResponseData();
        if(a != 0){
            rs.setCode(0);
            rs.setMsg("删除成功！");
        } else{
            rs.setCode(1);
            rs.setMsg("删除失败!");
        }
        return rs;
    }

    @Override
    public ResponseData findAll() {
        List<CustomerInfo> list = customerDao.findAll();
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;

    }

    @Override
    public ResponseData findById(Integer cid) {
        List<CustomerInfo>list = customerDao.findById(cid);
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        }else {
            rs.setCode(1);
            rs.setMsg("fail");
        }
        return rs;
    }

    @Override
    public ResponseData login(CustomerInfo customerInfo) {
        CustomerInfo customer = customerDao.login(customerInfo);
        ResponseData rs = new ResponseData();

        int m = customer.getCid();
        /**
         * 登陆判定条件
         * 首先判断customer_rolers表是否为空，若为空则通过cid进行权限赋值;
         * 若不为空则直接进行权限登陆
         */
        if(dao.list(m).size() != 0){
            if(customer != null){
                rs.setCode(0);
                rs.setMsg("ok");
                List<CustomerInfo>list = new ArrayList<>();
                list.add(customer);
                rs.setData(list);
            }else {
                rs.setCode(1);
                rs.setMsg("fail");
            }
        } else {
            CustomerRolers rolers = new CustomerRolers();
            rolers.setRid(customer.getCid());
            rolers.setCustomerid(customer.getCid());
            dao.add(rolers);

            PermissionRolers pr = new PermissionRolers();
            int[] arr = {5,6,9};
            for (int i : arr) {
                pr.setRid(customer.getCid());
                pr.setPid(i);
                prdao.addPermission(pr);
            }
            if(customer != null){
                rs.setCode(0);
                rs.setMsg("ok");
                List<CustomerInfo>list = new ArrayList<>();
                list.add(customer);
                rs.setData(list);
            }else {
                rs.setCode(1);
                rs.setMsg("fail");
            }
        }

        return rs;
    }


}
